<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\admin\model;

use api\common\model\CommonModel;
// use api\admin\model\FeedbackRecordModel;

class FeedbackModel extends CommonModel
{

    /**
     * [getFeedBackList 获取列表]
     * @return [type] [description]
     */
    public function getFeedBackList($userId)
    {
        $result = $this->alias("a")
            ->field('a.telephone,a.id,a.t_id,a.feedback_code,FROM_UNIXTIME(a.create_time, "%m-%d") as create_time,t.title')
            ->join('toilet t', 't.id = a.t_id', 'LEFT')
            ->where([
                't.user_id' => $userId,
                'a.status' => 1,
                 't.status' => 1
             ])
            ->paginate(10)->each(function($item){
                $item['best'] = (new FeedbackRecordModel)->where([
                    't_id'=>$item['t_id'],
                    'satisfaction'=>1,
                ])->count();
                $item['good'] = (new FeedbackRecordModel)->where([
                    't_id'=>$item['t_id'],
                    'satisfaction'=>2,
                ])->count();
                $item['bad'] = (new FeedbackRecordModel)->where([
                    't_id'=>$item['t_id'],
                    'satisfaction'=>3,
                ])->count();
            });
            return $result;
        // print_r($result->toArray());exit;

    }

    //获取当前用户反馈列表
    public function getFeedBackUserDetail($userId)
    {
        $result = $this->alias("a")
            ->field('a.telephone,a.thumbnail,a.id,a.t_id,a.feedback_code,FROM_UNIXTIME(a.create_time, "%m-%d") as create_time,t.title')
            ->join('toilet t', 't.id = a.t_id', 'LEFT')
            ->where([
                't.user_id' => $userId,
                'a.status' => 1,
                 't.status' => 1
             ])
            ->find();
            return $result;
        // print_r($result->toArray());exit;

    }



    /**
     * [getFeedBackDetail 获取当前详情]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getFeedBackDetail($id)
    {

        $detail              = $this->where(['id' => $id, 'status' => 1])->find();
        if (empty($detail)) {
            return null;
        }

        $toilet = (new ToiletModel)->where(['id'=>$detail['t_id'],'status'=>1])->find();
        if (empty($toilet)) {
            return null;
        }
        $detail['toilet_name'] = $toilet['title'];
        $detail['thumbnail'] = json_decode($detail['thumbnail'], true);
        $detail['tag'] = (new FeedbackTagModel())->field('tag_name,id')
            ->where(['user_id' => $toilet['user_id'], 'status' => 1])->select();
        return $detail;
    }



    //更新全局设置
    public function dataInsertFeedback($data,$userId)
    {
        $toilet = (new ToiletModel())
        ->field('id')->where('user_id',$userId)->select()->toArray();
        if (empty($toilet)) {
            return null;
        }
        $ids = '';
        foreach ($toilet as $key => $value) {
            $ids.=$value['id'].',';
            
        }
        $result = $this->where(['t_id'=>['in',rtrim($ids,',')]])->update($data);
        return $result;

    }

    //
    public function insertFeedbackData($tid,$userId)
    {
        //
        $data = ['t_id'=>$tid,'create_time'=>time()];

        $result = $this->alias("a")
            ->field('a.telephone,a.thumbnail')
            ->join('toilet t', 't.id = a.t_id')
            ->where([
                't.user_id' => $userId,
                'a.status' => 1,
                't.status' => 1
             ])
            ->whereNotNull('a.telephone')
            ->whereNotNull('a.thumbnail')
            ->find();
        if (!empty($result)) {
            $data = array_merge($data,$result->toArray());
        }
        $this->insert($data);
    }
    
}
